# 作者: YYJ
# 2025年07月13日15时38分02秒
# 2486249622@qq.com
import re

# 匹配开头字符
result = re.match('wangdao', 'wangdao.cn')
print(result.group())

# . 匹配任意单个字符
ret = re.match(".", "M")
print(ret.group())  # M
ret = re.match("t.o", "too")
print(ret.group())  # too
ret = re.match("t.o", "two")
print(ret.group())  # two

# [] 匹配范围内的单个字符
ret = re.match("[hH]", "hello Python")
print(ret.group())  # h
ret = re.match("[hH]", "Hello Python")
print(ret.group())  # H
ret = re.match("[hH]ello Python", "Hello Python")
print(ret.group())  # Hello Python

ret = re.match("[0-34]", "hello Python")
if ret:
    print(ret.group())
else:
    print("匹配失败")   # 匹配失败

# \d 匹配单个数字
ret = re.match("嫦娥\d号发射", "嫦娥3号发射")
print(ret.group())  # 嫦娥3号发射

# \w 匹配单个单词
ret = re.match("\wello", "hello Python")
print(ret.group())  # hello

# 匹配多个字符
ret = re.match("[A-Z][a-z]*", "M")
print(ret.group())  # M
ret = re.match("[A-Z][a-z]*", "MnnM")
print(ret.group())  # Mnn
ret = re.match("[A-Z][a-z]*", "Aabcdef")
print(ret.group())  # Aabcdef
# * 前一个字符可能出现任意多次

names = ["name1", "_name", "2_name", "__name__"]
for name in names:
    ret = re.match(r"[a-zA-Z_]+[\w]*", name)    # 要求匹配的字符串必须以字母或下划线开头，后面是任意多个小写字母
    if ret:
        print(name, "符合规范")
    else:
        print(name, "不符合规范")
# + 前一个字符至少出现一次

ret = re.match("[1-9]?[0-9]", "7")
print(ret.group())
ret = re.match("[1-9]?\d", "33")
print(ret.group())
ret = re.match("[1-9]?\d", "09")
print(ret.group()) # 0
# * 前一个字符最多出现一次

ret = re.match("[A-Za-z\d_]{6}", "abc_123_456")
if ret:
    print(ret.group())
else:
    print("不符合")

ret = re.match("[A-Za-z\d_]{8,20}", "abc_123_456")
print(ret.group())
# {m, n} 前一个字符出现m-n次